package com.blakebarrett.components
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.geom.ColorTransform;
	import flash.geom.Transform;
	
	import mx.containers.Canvas;
	import mx.controls.Image;

	public class Anaglyphizer extends Canvas
	{
		
		protected var bitmap1 : Bitmap;
		protected var bitmap2 : Bitmap;
		
		public function Anaglyphizer(bitmapData1:BitmapData, bitmapData2:BitmapData)
		{
			this.bitmap1 = new Bitmap(bitmapData1);
			this.bitmap2 = new Bitmap(bitmapData2);
			super();
		}
		
		override protected function createChildren () : void {
			
			bitmap1.x = 0;
			bitmap1.y = 0;
			
			bitmap2.x = 0;
			bitmap2.y = 0;
			
			var image1 : Image = new Image();
			image1.addChild(bitmap1);
			transformImage(image1, 0, 255, 1, 2, 1);
			
			var image2 : Image = new Image();
			image2.addChild(bitmap2);
			transformImage(image2, 255, 0, 0.5, 1, 2);
			
			this.addChild( image1 );
			this.addChild( image2 );
			
			super.createChildren();
		}
		
		protected function transformImage (image:Image, red:Number, blue:Number, alpha:Number, redMultiplier:Number, blueMultiplier:Number):void {
			var colorTrans : ColorTransform = new ColorTransform();
			colorTrans.redOffset = Math.max(red - blue, 0);
			colorTrans.redMultiplier = redMultiplier;
			colorTrans.greenMultiplier = .5;
			colorTrans.blueOffset = Math.max(blue - red, 0);
			colorTrans.blueMultiplier = blueMultiplier;
			colorTrans.alphaMultiplier = alpha;
			var trans:Transform = new Transform(image);
			trans.colorTransform = colorTrans;
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
		}
	}
}